package StackDemo;

import java.util.Scanner;

public class ArraysStackDemo {
    public static void main(String[] args) {
        Stack numStack = new Stack(10);
        Stack operStack = new Stack(20);
        String str="20+4";
        int num1;
        int num2;
        int oper;
        int res;
        char ch ;
        int index=0;
        String keepNum="";
        while(true){
            //获取一个字节
            ch = str.substring(index,index+1).charAt(0);
            //判断是否为运算符
            if(operStack.isOper(ch)){
                //如果运算符栈不为空
                if(!operStack.isEmpty()){
                    //从栈中取出一个符号和ch比较如果ch<=取出的符号
                        if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
                            num1=numStack.pop();
                            num2=numStack.pop();
                            oper=operStack.pop();
                            res=numStack.cal(num1,num2,oper);
                            numStack.push(res);
                            operStack.push(ch);//当前操作符添加入栈
                        }else {//否则就添加此符号

                            operStack.push(ch);
                        }
                }else{
                    operStack.push(ch);
                }
            }else{
                /*numStack.push(ch-48);
               如果计算两位数此方法不可行
               */
                keepNum+=ch;
                if(index==str.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else{
                    char cc = str.substring(index+1,index+2).charAt(0);
                if(operStack.isOper(cc)){
                    numStack.push(Integer.parseInt(keepNum));
                    keepNum="";
                }
                }
            }
                index++;
            if(index>=str.length())
                break;
        }
        while(true){
            if(operStack.isEmpty())
                break;
            num1=numStack.pop();
            num2=numStack.pop();
            oper=operStack.pop();
            res=numStack.cal(num1,num2,oper);
            numStack.push(res);
        }
        System.out.println("表达式"+str+"="+numStack.pop());
    /*    Stack st = new Stack(5);
        Scanner sc =new Scanner(System.in);
        String key = "";
        boolean bool = true;
        while(bool){
            System.out.println("show显示");
            System.out.println("exit退出");
            System.out.println("push添加");
            System.out.println("pop取数据");
           key= sc.next();
           switch (key){
               case "show":
                   st.list();
                   break;
               case "exit":
                   bool=false;
                   break;
               case "pop":
                   int value=st.pop();
                   System.out.println("取出的数据："+value);
                   break;
               case "push":
                   System.out.println("请输入添加的数字");
                   int values = sc.nextInt();
                   st.push(values);
                   break;
           }

        }*/
    }
}
class Stack{
    int[] arr ;//栈
    int maxSize;//最大值
    int top=-1;//栈顶
    Stack(int num){
        maxSize=num;
        arr= new int[maxSize];
    }
    //查看栈顶元素
    public int peek(){
        return arr[top];
    }
    //判断栈满
    public boolean isFull(){
        return maxSize-1==top;
    }
    //判断栈是否为空
    public boolean isEmpty(){
        return top==-1;
    }
    //入栈
    public void push(int i){
        if(isFull()){
            System.out.println("栈满无法添加");
        return ;}
        top++;
        arr[top]=i;
    }
    //出栈
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        int value = arr[top];
       // System.out.println("top="+top+"value+"+value);
        top--;
        return value;
    }
    //显示所有数据
    public void list(){
        if(isEmpty()){
            System.out.println("数据空，显示失败！");
            return;
        }
        for(int i=top;i>=0;i--){
            System.out.println("stack["+arr[i]+"]");
        }
        System.out.println();
    }
    //返回运算符的优先级
    /*
    1表示一级运算
    0表示二级运算
    -1表示错误
     */
    public int priority(int oper){
        if(oper=='*'||oper=='/'){
            return 1;
        }else if(oper=='+'||oper=='-'){
            return 0;
        }else{
            return -1;
        }
    }
    //判断是否为一个运算符
    public boolean isOper(char val){
        return val=='*'||val=='/'||val=='+'||val=='-';

    }
    //计算方法
    public int cal(int num1,int num2,int oper) {
        int res=0;
        switch (oper) {
            case '+':
                res=num2+num1;
                break;
            case '-':
                res=num2-num1;
                break;
            case '*':
                res=num2*num1;
                break;
            case '/':
                res=num2/num1;
                break;
               default:
                   break;

        }
        return res;
    }
}